package com.huan.table;

import com.google.common.collect.Table;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

public class TableReducer extends Reducer<Text,TableBean,TableBean, NullWritable> {

    @Override
    protected void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {

        //TODO 存储所有的订单集合
        ArrayList<TableBean> orderBean = new ArrayList<>();
        //TODO 存储产品信息
        TableBean pdBean = new TableBean();

        for (TableBean tableBean : values) {

            if ("order".equals( tableBean.getFlag() )){ //TODO 订单表

                TableBean tmpBean = new TableBean();

                try { //TODO 将tableBean的数据拷贝到tmpBean里面去
                    BeanUtils.copyProperties( tmpBean, tableBean);
                    orderBean.add( tmpBean );
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }else{
                try {
                    BeanUtils.copyProperties( pdBean,tableBean );
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }

        }
        for (TableBean tablebean : orderBean) {
            tablebean.setName( pdBean.getName() );

            context.write( tablebean,NullWritable.get() );
        }
    }
}
